package com.cardone.platform.authority.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.dto.RoleDto;
import com.cardone.platform.authority.service.RoleService;

/**
 * 角色
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/authority/role/")
@Controller
public class RoleController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 角色：删除
	 *
	 * @param deleteRole
	 *          角色对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final RoleDto deleteRole) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(RoleService.class).deleteByIds(deleteRole);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleController.log);
				}
			}
		};
	}

	/**
	 * 角色：分页查询
	 *
	 * @param findRole
	 *          角色对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final RoleDto findRole, final Model model) {
		final RoleDto role = ContextHolder.getBean(RoleService.class).findById(RoleDto.class, findRole);

		model.addAttribute("role", role);
	}

	/**
	 * 角色：编辑
	 *
	 * @param findRole
	 *          角色对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<RoleDto>> findByIdJson(final RoleDto findRole, final Model model) {
		return new Callable<ReturnDataDto<RoleDto>>() {
			@Override
			public ReturnDataDto<RoleDto> call() throws Exception {
				try {
					final RoleDto role = ContextHolder.getBean(RoleService.class).findById(RoleDto.class, findRole);

					return ReturnDataUtils.newReturnData(role);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleController.log);
				}
			}
		};
	}

	/**
	 * 角色：查询
	 *
	 * @param findListRole
	 *          角色对象
	 * @return 角色对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<RoleDto>>> findListByLikeCodeJson(final RoleDto findListRole) {
		return new Callable<ReturnDataDto<List<RoleDto>>>() {
			@Override
			public ReturnDataDto<List<RoleDto>> call() throws Exception {
				try {
					final List<RoleDto> roleList = ContextHolder.getBean(RoleService.class).findListByLikeCode(RoleDto.class, findListRole);

					return ReturnDataUtils.newReturnData(roleList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleController.log);
				}
			}
		};
	}

	/**
	 * 角色：分页查询
	 *
	 * @param paginationRole
	 *          角色对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<RoleDto>>> paginationByLikeCodeJson(final RoleDto paginationRole) {
		return new Callable<ReturnDataDto<PaginationDto<RoleDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<RoleDto>> call() throws Exception {
				try {
					final PaginationDto<RoleDto> rolePagination = ContextHolder.getBean(RoleService.class).paginationByLikeCode(RoleDto.class, paginationRole);

					return ReturnDataUtils.newReturnData(rolePagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleController.log);
				}
			}
		};
	}

	/**
	 * 角色：分页查询
	 *
	 * @param paginationRole
	 *          角色对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final RoleDto paginationRole, final Model model) {
		final PaginationDto<RoleDto> rolePagination = ContextHolder.getBean(RoleService.class).paginationByLikeCode(RoleDto.class, paginationRole);

		model.addAttribute("rolePagination", rolePagination);
	}

	/**
	 * 角色：保存
	 *
	 * @param saveRole
	 *          角色对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final RoleDto saveRole) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(RoleService.class).saveByIdOrCode(RoleDto.class, saveRole);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleController.log);
				}
			}
		};
	}
}
